<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="MySQL基础">
<meta property="og:type" content="article">
<meta property="og:title" content="Java日拱一卒-day12">
<meta property="og:url" content="http://yoursite.com/2020/04/29/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day12/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="MySQL基础">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-29T03:10:36.000Z">
<meta property="article:modified_time" content="2020-05-17T18:19:49.446Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/04/29/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day12/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Java日拱一卒-day12 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/04/29/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day12/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java日拱一卒-day12
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-04-29 11:10:36" itemprop="dateCreated datePublished" datetime="2020-04-29T11:10:36+08:00">2020-04-29</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-18 02:19:49" itemprop="dateModified" datetime="2020-05-18T02:19:49+08:00">2020-05-18</time>
              </span>

          
            <div class="post-description">MySQL基础</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="JDBC基础"><a href="#JDBC基础" class="headerlink" title="JDBC基础"></a>JDBC基础</h2><p><code>JDBC</code>全称为<code>Java Database Connectivity</code>，是一种可以执行<code>SQL</code>语句的<code>Java API</code>，通过使用<code>JDBC</code>，就可以使用同一种<code>API</code>访问不同的数据库系统。简单来说，<code>JDBC</code>具有以下三个功能：</p>
<ul>
<li>建立与数据库的连接</li>
<li>执行<code>SQL</code>语句</li>
<li>获取<code>SQL</code>语句的执行结果</li>
</ul>
<h2 id="关系型数据库基本概念"><a href="#关系型数据库基本概念" class="headerlink" title="关系型数据库基本概念"></a>关系型数据库基本概念</h2><p>严格来说，数据库，即<code>Database</code>仅仅是存放用户数据的地方，当用户访问、操作数据库的数据时，就需要数据库管理系统(<code>Database Management System,DBMS</code>)的帮助。习惯上常把数据库和数据库管理系统笼统称为数据库。通常所说的数据库既包括存储用户数据的部分，也包括管理数据库的管理系统。<br><code>DBMS</code>是所有数据的知识库，负责管理数据的存储、安全、一致性、并发、恢复和访问等操作。<code>DBMS</code>有一个数据字典（或称系统表），用于存储它拥有的每个事物的相关信息，例如名字、结构、位置和类型，这些关于数据的数据被称为<code>元数据</code>(<code>metadata</code>).<br>在数据库历史中，按时间顺序主要出现了如下几种数据库类型：</p>
<ul>
<li>网状型数据库</li>
<li>层次型数据库</li>
<li>关系数据库</li>
<li>面向对象数据库</li>
</ul>
<p>-<br>以上四种数据库，关系数据库理论最成熟，应用最广泛。对于关系型数据库而言，最基本的数据存储单元就是数据表，可以把数据表想象成由行和列组成的表格，每一行被称为一条记录，每一列被称为一个字段。每个数据表应指定一个特殊列，该特殊列的值可以唯一地标识此行的数据，则该特殊列被称为主键列。<br><code>MySQL</code>数据库的一个实例可以同时包含多个数据库，<code>MySQL</code>使用如下命令查看当前实力下包含多少个数据库：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">databases</span>;</span><br></pre></td></tr></table></figure>

<ul>
<li>创建新的数据库：</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">database</span> [<span class="keyword">IF</span> <span class="keyword">NOT</span> <span class="keyword">EXISTS</span>] 数据库名;</span><br></pre></td></tr></table></figure>

<ul>
<li>删除指定数据库：</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">database</span> 数据库名;</span><br></pre></td></tr></table></figure>

<ul>
<li>进入指定数据库：</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">use</span> 数据库名;</span><br></pre></td></tr></table></figure>

<ul>
<li>查询该数据库下包含多少数据表：</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> <span class="keyword">table</span>;</span><br></pre></td></tr></table></figure>

<ul>
<li>查看指定表的表结构</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">desc 表名;</span><br></pre></td></tr></table></figure>

<h2 id="SQL语言基础"><a href="#SQL语言基础" class="headerlink" title="SQL语言基础"></a><code>SQL</code>语言基础</h2><h3 id="创建、查看、删除表"><a href="#创建、查看、删除表" class="headerlink" title="创建、查看、删除表"></a>创建、查看、删除表</h3><ul>
<li>标准的创建表的语法如下：</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> [模式名.]表名</span><br><span class="line">｛</span><br><span class="line">    <span class="comment"># 可以有多个列定义</span></span><br><span class="line">    columnName1 datatype [ <span class="keyword">DEFAULT</span> expr],</span><br><span class="line">    ……</span><br><span class="line">｝;</span><br></pre></td></tr></table></figure>

<p>例如下面的建表语句：</p>
<p>创建stu学生表（编号[数值类型]、姓名、性别、出生年月、考试成绩[浮点<br>型]），建表的语法：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> <span class="keyword">if</span> <span class="keyword">exists</span> stu;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> stu(</span><br><span class="line"><span class="keyword">id</span> <span class="built_in">int</span> primary <span class="keyword">key</span> auto_increment, <span class="comment">-- 将id设置为主键(唯一且不能为</span></span><br><span class="line">空),并且设置为自增</span><br><span class="line"><span class="keyword">name</span> <span class="built_in">varchar</span>(<span class="number">50</span>),</span><br><span class="line">gender <span class="built_in">varchar</span>(<span class="number">10</span>),</span><br><span class="line">birthday <span class="built_in">date</span>,</span><br><span class="line">score <span class="keyword">double</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<ul>
<li>删除表</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">use</span> mydb1;</span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> <span class="keyword">if</span> <span class="keyword">exists</span> stu;</span><br></pre></td></tr></table></figure>

<ul>
<li>查看stu表结构</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">desc stu;</span><br></pre></td></tr></table></figure>

<h3 id="新增、更新、删除表记录"><a href="#新增、更新、删除表记录" class="headerlink" title="新增、更新、删除表记录"></a>新增、更新、删除表记录</h3><ul>
<li>往学生表(stu)中插入记录(数据)<br>语法为：</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> 表名(列名<span class="number">1</span>,列名<span class="number">2</span>,列名<span class="number">3.</span>..) <span class="keyword">VALUES</span>(值<span class="number">1</span>,值<span class="number">2</span>,值<span class="number">3.</span>..);</span><br></pre></td></tr></table></figure>

<p>示例如下：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 如果是在cmd中执行插入记录的语句,先 set names gbk; 再插入记录!</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu(<span class="keyword">id</span>,<span class="keyword">name</span>,gender,birthday,score) <span class="keyword">value</span></span><br><span class="line">(<span class="literal">null</span>,<span class="string">'tom'</span>,<span class="string">'male'</span>,<span class="string">'1985-1-1'</span>,<span class="number">78</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu <span class="keyword">value</span>(<span class="literal">null</span>,<span class="string">'吴忠友'</span>,<span class="string">'female'</span>,<span class="string">'1985-2-2'</span>,<span class="number">88</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu <span class="keyword">value</span>(<span class="literal">null</span>,<span class="string">'子虚'</span>,<span class="string">'male'</span>,<span class="string">'1999-3-3'</span>,<span class="number">68</span>);</span><br></pre></td></tr></table></figure>

<p>提示：</p>
<ol>
<li>当为所有列插入值时，可以省写列名，但值的个数和顺序必须和声明时列的个数和顺序保持一致！</li>
<li>SQL语句中的值为字符串或日期时，值的两边要加上单引号（有的版本的数据库双引<br>号也可以，但推荐使用单引号）。</li>
<li>(针对cmd窗口)在插入数据之前，先设置编码：set names gbk;</li>
</ol>
<ul>
<li>查询stu表所有学生的信息<br>语法：</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 列名 | * <span class="keyword">FROM</span> 表名</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> stu;</span><br></pre></td></tr></table></figure>

<ul>
<li>修改stu表中所有学生的成绩，加10分特长分</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">UPDATE</span> 表名 <span class="keyword">SET</span> 列=值,列=值,列=值...[<span class="keyword">WHERE</span>子句];</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> stu <span class="keyword">set</span> score=score+<span class="number">10</span>;</span><br></pre></td></tr></table></figure>

<ul>
<li>删除stu表中所有的记录</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> 表名 [<span class="keyword">where</span>条件]</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> stu;</span><br></pre></td></tr></table></figure>

<p>仅删除符合条件的：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> stu <span class="keyword">where</span> <span class="keyword">id</span>&gt;<span class="number">1</span>;</span><br></pre></td></tr></table></figure>

<h3 id="查询表记录"><a href="#查询表记录" class="headerlink" title="查询表记录"></a>查询表记录</h3><h4 id="基础查询"><a href="#基础查询" class="headerlink" title="基础查询"></a>基础查询</h4><p><code>SELECT</code>语句用于从表中选取数据。结果被存储在一个结果表中（称为结果集）。语法：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 列名称 | * <span class="keyword">FROM</span> 表名</span><br></pre></td></tr></table></figure>

<p>在select之后、列名之前，使用DISTINCT 剔除重复的记录</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> dept,job <span class="keyword">from</span> emp;</span><br></pre></td></tr></table></figure>

<h4 id="WHERE子句查询"><a href="#WHERE子句查询" class="headerlink" title="WHERE子句查询"></a><code>WHERE</code>子句查询</h4><p>WHERE子句查询语法：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 列名称 | * <span class="keyword">FROM</span> 表名称 <span class="keyword">WHERE</span> 列 运算符 值</span><br></pre></td></tr></table></figure>

<p>WHERE子句后面跟的是条件，条件可以有多个，多个条件之间用连接词（or | and）进行连接<br>下面的运算符可在 WHERE 子句中使用：</p>
<table>
<thead>
<tr>
<th align="center">操作符</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">=</td>
<td align="center">等于</td>
</tr>
<tr>
<td align="center">&lt;&gt;或!=</td>
<td align="center">不等于</td>
</tr>
<tr>
<td align="center">&gt;</td>
<td align="center">大于</td>
</tr>
<tr>
<td align="center">&lt;</td>
<td align="center">小于</td>
</tr>
<tr>
<td align="center">&gt;=</td>
<td align="center">大于等于</td>
</tr>
<tr>
<td align="center">&lt;=</td>
<td align="center">小于等于</td>
</tr>
<tr>
<td align="center">BETWEEN</td>
<td align="center">在某个范围</td>
</tr>
<tr>
<td align="center">LIKE</td>
<td align="center">搜索某个模式</td>
</tr>
</tbody></table>
<ul>
<li>查询emp表中薪资大于3000的所有员工，显示员工姓名、薪资</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">name</span>,sal <span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> sal&gt;<span class="number">3000</span>;</span><br></pre></td></tr></table></figure>

<h4 id="模糊查询"><a href="#模糊查询" class="headerlink" title="模糊查询"></a>模糊查询</h4><p><code>LIKE</code>操作符用于在<code>WHERE</code>子句中搜索列中的指定模式。<br>可以和通配符（<code>%</code>、<code>_</code>）配合使用，其中<code>%</code>表示0或多个任意的字符。<code>_</code>表示<br>一个任意的字符。<br>语法：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 列 | * <span class="keyword">FROM</span> 表名 <span class="keyword">WHERE</span> 列名 <span class="keyword">LIKE</span> 值</span><br></pre></td></tr></table></figure>

<p>例如：查询emp表中姓名中包含”涛”字的员工，显示员工姓名。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">name</span> <span class="keyword">from</span> emp <span class="keyword">where</span> <span class="keyword">name</span> <span class="keyword">like</span> <span class="string">'%涛%'</span>;</span><br></pre></td></tr></table></figure>

<h4 id="多行函数查询"><a href="#多行函数查询" class="headerlink" title="多行函数查询"></a>多行函数查询</h4><p>多行函数又称聚合（聚集）函数，根据某一列或者所有列进行统计。常用多行函数如下<br>|多行函数|作用|<br>|:-:|:-:|<br>|COUNT(列名| *)|统计结果集中指定列的记录的行数|<br>|MAX(列名)|统计结果集中某一列值中的最大值|<br>|MIN(列名)|统计结果集中某一列值中的最小值|<br>|SUM(列名)|统计结果集中某一列所有值的和|<br>|AVG(列名)|统计结果集中某一列的值的平均值|<br>提示：</p>
<ol>
<li>多行函数不能用在<code>where</code>子句中</li>
<li>多行函数和是否分组有关，分组与否会直接影响到多行函数的执行结果</li>
<li>多行函数在统计时会对<code>null</code>进行过滤，直接将<code>null</code>丢弃，不参与统计</li>
</ol>
<p>例如：统计emp表中薪资大于3000的员工个数</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">count</span>(*) <span class="keyword">from</span> emp <span class="keyword">where</span> sal&gt;<span class="number">3000</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">count</span>(<span class="keyword">id</span>) <span class="keyword">from</span> emp <span class="keyword">where</span> sal&gt;<span class="number">3000</span>;</span><br></pre></td></tr></table></figure>

<p>求emp表中的最高薪资</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> sal <span class="keyword">from</span> emp; <span class="comment">-- 查询emp表中的所有薪资</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">max</span>(sal) <span class="keyword">from</span> emp; <span class="comment">-- 统计emp表中的最高薪资</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">min</span>(sal) <span class="keyword">from</span> emp; <span class="comment">-- 统计emp表中的最低薪资</span></span><br></pre></td></tr></table></figure>

<p>统计emp表中所有员工的薪资总和(不包含奖金)</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">sum</span>(sal) <span class="keyword">from</span> emp; <span class="comment">-- 统计sal这一列中的所有值的和</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">sum</span>(bonus) <span class="keyword">from</span> emp; <span class="comment">-- 统计bonus这一列中的所有值的和</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">sum</span>(sal)+<span class="keyword">sum</span>(bonus) <span class="keyword">from</span> emp; <span class="comment">-- 统计 薪资总和+奖金总和</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">sum</span>(sal+bonus) <span class="keyword">from</span> emp; <span class="comment">-- 因为bonus列中有null值,所以结果有误</span></span><br><span class="line">差</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">sum</span>(sal+<span class="keyword">ifnull</span>(bonus,<span class="number">0</span>)) <span class="keyword">from</span> emp; <span class="comment">-- 处理null值,结果正确</span></span><br></pre></td></tr></table></figure>

<h4 id="分组查询"><a href="#分组查询" class="headerlink" title="分组查询"></a>分组查询</h4><p><code>GRUOP BY</code>语句根据一个或多个列对结果集进行分组。在分组的列上我们可以使用<code>COUNT</code>、<code>SUM</code>、<code>AVG</code>、<code>MAX</code>、<code>MIN</code>等函数。<br>语法：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 列| * <span class="keyword">FROM</span> 表名 [<span class="keyword">WHERE</span> 子句] <span class="keyword">GROUP</span> <span class="keyword">BY</span> 列;</span><br></pre></td></tr></table></figure>

<p>对emp表按照部门对员工进行分组，查看分组后效果。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> emp <span class="keyword">group</span> <span class="keyword">by</span> dept; <span class="comment">-- 按照部门分组, 部门相同的是一组</span></span><br></pre></td></tr></table></figure>

<h4 id="排序查询"><a href="#排序查询" class="headerlink" title="排序查询"></a>排序查询</h4><p>使用<code>ORDER BY</code>子句将结果集中记录根据指定的列排序后再返回<br>语法：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 列名 <span class="keyword">FROM</span> 表名 <span class="keyword">ORDER</span> <span class="keyword">BY</span> 列名 [<span class="keyword">ASC</span>|<span class="keyword">DESC</span>];</span><br></pre></td></tr></table></figure>

<p>ASC(默认)升序，即从低到高；DESC 降序，即从高到低。</p>
<h4 id="分页查询"><a href="#分页查询" class="headerlink" title="分页查询"></a>分页查询</h4><p>在<code>MySQL</code>中，通过<code>limit</code>进行分页查询</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">LIMIT (页码-1)*每页显示的记录数,每页显示记录数</span><br></pre></td></tr></table></figure>

<p>或者也可以理解为</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">LIMIT [记录从0开始的索引],[取的条数]</span><br></pre></td></tr></table></figure>

<p>查询emp表中的所有记录，分页显示：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> emp <span class="keyword">limit</span> <span class="number">0</span>,<span class="number">3</span>;  <span class="comment">-- 每页3条，查询第1页</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> emp <span class="keyword">limit</span> <span class="number">3</span>,<span class="number">3</span>;  <span class="comment">-- 每页3条，查询第2页</span></span><br><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> emp <span class="keyword">limit</span> <span class="number">6</span>,<span class="number">3</span>;  <span class="comment">-- 每页3条，查询第3页</span></span><br></pre></td></tr></table></figure>

<p>(页码-1)*每页显示的记录数实际上就是要查的这一页的第一个</p>
<h4 id="其他函数"><a href="#其他函数" class="headerlink" title="其他函数"></a>其他函数</h4><table>
<thead>
<tr>
<th align="center">函数名</th>
<th align="center">解释说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">curdate()</td>
<td align="center">获取当前日期，年月日</td>
</tr>
<tr>
<td align="center">curtime()</td>
<td align="center">获取当前时间，时分秒</td>
</tr>
<tr>
<td align="center">sysdate()/now()</td>
<td align="center">获取当前日期时间，年月日时分秒</td>
</tr>
<tr>
<td align="center">year(date)</td>
<td align="center">返回date的年份</td>
</tr>
<tr>
<td align="center">month(date)</td>
<td align="center">返回date的月份</td>
</tr>
<tr>
<td align="center">day(date)</td>
<td align="center">返回date的天数</td>
</tr>
<tr>
<td align="center">hour(date)</td>
<td align="center">返回date的小时</td>
</tr>
<tr>
<td align="center">minute(date)</td>
<td align="center">返回date的分钟</td>
</tr>
<tr>
<td align="center">second(date)</td>
<td align="center">返回date的秒</td>
</tr>
<tr>
<td align="center">CONCAT(s1,s2…)</td>
<td align="center">将s1,s2等多个字符串合并为一个字符串</td>
</tr>
<tr>
<td align="center">CONCAT_WS(x,s1,s2,…)</td>
<td align="center">同上，但是每个字符串之间加上x作为分隔符</td>
</tr>
</tbody></table>
<p>查询emp表中所有在1993和1995年之间出生的员工，显示姓名、出生日<br>期。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">name</span>,birthday <span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> birthday&gt;=<span class="string">'1993-1-1'</span> <span class="keyword">and</span> birthday&lt;=<span class="string">'1995-12-31'</span>;</span><br><span class="line"><span class="comment">-- 或 将birthday中的年份取出来, 和1993及1995进行比较!</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">name</span>,birthday <span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> <span class="keyword">year</span>(birthday)&gt;=<span class="number">1993</span> <span class="keyword">and</span> <span class="keyword">year</span>(birthday)&lt;=<span class="number">1995</span>;</span><br></pre></td></tr></table></figure>

<h3 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h3><h4 id="数值类型"><a href="#数值类型" class="headerlink" title="数值类型"></a>数值类型</h4><p><code>MySQL</code>中支持多种整型<br>类型|说明<br>:-:|:-:<br>tinyint|占用1个字节，相当于java的byte<br>smallint|占用2个字节，相当于java的short<br>int|占用4个字节，相当于java的int<br>bigint|占用8个字节，相当于，java的long<br>其次是浮点类型</p>
<table>
<thead>
<tr>
<th align="center">类型</th>
<th align="center">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">float</td>
<td align="center">4字节单精度浮点型，相当于java的float</td>
</tr>
<tr>
<td align="center">double</td>
<td align="center">8字节双精度浮点型，相当于java的double</td>
</tr>
</tbody></table>
<h4 id="字符类型"><a href="#字符类型" class="headerlink" title="字符类型"></a>字符类型</h4><ul>
<li><ol>
<li>char(n)定长字符串，最长255个字符。n表示字符数，例如：</li>
</ol>
</li>
</ul>
<p>创建user表，指定用户名为<code>char</code>类型，字符长度不超过10</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> <span class="keyword">user</span>(</span><br><span class="line">    username <span class="built_in">char</span>(<span class="number">10</span>)</span><br><span class="line">    ...</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>定长会导致插入的数据长度小于定长时，剩余空间会被浪费</p>
<ul>
<li><ol start="2">
<li>varchar(n)变长字符串，最长不超过65535个字节，n表示字符数，一般超过255个字符会使用text类型</li>
</ol>
</li>
</ul>
<blockquote>
<p>iso8859-1码表：一个字符占用1个字节，1<em>n &lt;= 65535， n最多等于 65535<br>utf8码表：一个中文汉字占用3个字节，3</em>n&lt;=65535，n最多等于 65535/3<br>GBK码表：一个中文汉字占用2个字节，2*n&lt;65535，n最多等于 65535/2</p>
</blockquote>
<p>创建user表，指定用户名为varchar类型，长度不超过10</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> <span class="keyword">user</span>(</span><br><span class="line">    username <span class="built_in">varchar</span>(<span class="number">10</span>)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>不定长是指插入的数据小于指定长度时，剩余空间可以留给别的数据使用</p>
<ul>
<li><ol start="3">
<li>大文本（长文本）类型</li>
</ol>
</li>
</ul>
<p>最长65535个字节，一般超过255个字符列的会使用text<br>创建user表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> <span class="keyword">user</span>(</span><br><span class="line">    <span class="keyword">resume</span> <span class="built_in">text</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p><code>text</code>也分很多种，其中<code>bigtext</code>存储数据的长度约为4GB</p>
<h4 id="日期类型"><a href="#日期类型" class="headerlink" title="日期类型"></a>日期类型</h4><table>
<thead>
<tr>
<th align="center">类型</th>
<th align="center">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">date</td>
<td align="center">年月日</td>
</tr>
<tr>
<td align="center">datetime</td>
<td align="center">年月日时分秒</td>
</tr>
<tr>
<td align="center">timestamp</td>
<td align="center">时间戳</td>
</tr>
<tr>
<td align="center"><code>timestamp</code>，实际上存储的是一个时间毫秒值，与<code>datetime</code>存储日期格式相同，区别在于</td>
<td align="center"></td>
</tr>
</tbody></table>
<ul>
<li><code>timestamp</code>最大表示2038年，而<code>datetime</code>范围是1000~9999</li>
<li><code>timestamp</code>在插入、修改数据时，可以自动更新称系统当前时间</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/04/24/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day11/" rel="prev" title="Java日拱一卒-day11">
      <i class="fa fa-chevron-left"></i> Java日拱一卒-day11
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/05/02/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day13/" rel="next" title="Java日拱一卒-day13">
      Java日拱一卒-day13 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#JDBC基础"><span class="nav-number">1.</span> <span class="nav-text">JDBC基础</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#关系型数据库基本概念"><span class="nav-number">2.</span> <span class="nav-text">关系型数据库基本概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SQL语言基础"><span class="nav-number">3.</span> <span class="nav-text">SQL语言基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建、查看、删除表"><span class="nav-number">3.1.</span> <span class="nav-text">创建、查看、删除表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#新增、更新、删除表记录"><span class="nav-number">3.2.</span> <span class="nav-text">新增、更新、删除表记录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查询表记录"><span class="nav-number">3.3.</span> <span class="nav-text">查询表记录</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#基础查询"><span class="nav-number">3.3.1.</span> <span class="nav-text">基础查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WHERE子句查询"><span class="nav-number">3.3.2.</span> <span class="nav-text">WHERE子句查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#模糊查询"><span class="nav-number">3.3.3.</span> <span class="nav-text">模糊查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#多行函数查询"><span class="nav-number">3.3.4.</span> <span class="nav-text">多行函数查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#分组查询"><span class="nav-number">3.3.5.</span> <span class="nav-text">分组查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#排序查询"><span class="nav-number">3.3.6.</span> <span class="nav-text">排序查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#分页查询"><span class="nav-number">3.3.7.</span> <span class="nav-text">分页查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#其他函数"><span class="nav-number">3.3.8.</span> <span class="nav-text">其他函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据类型"><span class="nav-number">3.4.</span> <span class="nav-text">数据类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#数值类型"><span class="nav-number">3.4.1.</span> <span class="nav-text">数值类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#字符类型"><span class="nav-number">3.4.2.</span> <span class="nav-text">字符类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#日期类型"><span class="nav-number">3.4.3.</span> <span class="nav-text">日期类型</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
